﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableEditBoxControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands;

public class CApCableEditBoxControl : 
  UserControl,
  ICApCableEditBoxControl,
  ICApCableFocusableControl,
  IDisposable,
  IComponentConnector
{
  private ITxDisplayableObject m_displayableObject;
  private readonly TxObjEditBoxControl m_editBox;
  private bool _contentLoaded;

  public CApCableEditBoxControl()
  {
    this.InitializeComponent();
    this.m_editBox = this.Content as TxObjEditBoxControl;
  }

  public void Dispose() => this.UnregisterFromObjectEvents();

  public void InitializeControl(ITxValidator objectValidator)
  {
    if (objectValidator == null)
      throw new ArgumentNullException(nameof (objectValidator));
    this.m_editBox.SetValidator(objectValidator);
  }

  public bool SetFocus() => this.m_editBox.Focus();

  public ITxDisplayableObject DisplayableObject
  {
    get => this.m_displayableObject;
    set
    {
      if (value == this.m_displayableObject)
        return;
      this.UnregisterFromObjectEvents();
      this.m_displayableObject = value;
      this.RegisterToObjectEvents();
      this.UpdateText();
      CApCableEditBoxControlEventHandler objectChanged = this.ObjectChanged;
      if (objectChanged == null)
        return;
      objectChanged((ICApCableEditBoxControl) this);
    }
  }

  public event CApCableEditBoxControlEventHandler ObjectChanged;

  private void SetObject(ITxDisplayableObject displayableObject)
  {
    CApCableUtility.HighlightObject(displayableObject);
    this.DisplayableObject = displayableObject;
  }

  private void UnsetObject()
  {
    CApCableUtility.UnHighlightObject(this.DisplayableObject);
    this.DisplayableObject = (ITxDisplayableObject) null;
  }

  private void UpdateText()
  {
    this.m_editBox.Text = ((ITxObject) this.m_displayableObject)?.Name ?? string.Empty;
  }

  private void GiveFocus()
  {
    if (!(Keyboard.FocusedElement is FrameworkElement focusedElement))
      return;
    focusedElement.MoveFocus(new TraversalRequest(this.GetFocusNavigationDirection()));
  }

  private FocusNavigationDirection GetFocusNavigationDirection()
  {
    return !Keyboard.IsKeyDown(Key.LeftShift) && !Keyboard.IsKeyDown(Key.RightShift) ? FocusNavigationDirection.Next : FocusNavigationDirection.Previous;
  }

  private void RegisterToObjectEvents()
  {
    if (this.m_displayableObject == null)
      return;
    ((ITxObject) this.m_displayableObject).NameChanged += new TxObject_NameChangedEventHandler(this.DisplayableObject_NameChanged);
    ((ITxObject) this.m_displayableObject).Deleted += new TxObject_DeletedEventHandler(this.DisplayableObject_Deleted);
  }

  private void UnregisterFromObjectEvents()
  {
    if (this.m_displayableObject == null)
      return;
    ((ITxObject) this.m_displayableObject).NameChanged -= new TxObject_NameChangedEventHandler(this.DisplayableObject_NameChanged);
    ((ITxObject) this.m_displayableObject).Deleted -= new TxObject_DeletedEventHandler(this.DisplayableObject_Deleted);
  }

  private void EditBox_GotFocus(object sender, RoutedEventArgs e)
  {
    CApCableUtility.HighlightObject(this.DisplayableObject);
  }

  private void EditBox_LostFocus(object sender, RoutedEventArgs e)
  {
    CApCableUtility.UnHighlightObject(this.DisplayableObject);
  }

  private void EditBox_PreviewKeyDown(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Tab)
      return;
    this.GiveFocus();
    e.Handled = true;
  }

  private void EditBox_KeyUp(object sender, KeyEventArgs e)
  {
    switch (e.Key)
    {
      case Key.Back:
      case Key.Delete:
        this.UnsetObject();
        break;
    }
  }

  private void EditBox_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (args.Object is ITxDisplayableObject displayableObject && args.IsValidObjectPicked)
      this.SetObject(displayableObject);
    else
      this.UnsetObject();
  }

  private void DisplayableObject_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    this.UpdateText();
  }

  private void DisplayableObject_Deleted(object sender, TxObject_DeletedEventArgs args)
  {
    this.DisplayableObject = (ITxDisplayableObject) null;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/editboxcontrols/capcableeditboxcontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    if (connectionId == 1)
    {
      ((UIElement) target).GotFocus += new RoutedEventHandler(this.EditBox_GotFocus);
      ((UIElement) target).LostFocus += new RoutedEventHandler(this.EditBox_LostFocus);
      ((UIElement) target).PreviewKeyDown += new KeyEventHandler(this.EditBox_PreviewKeyDown);
      ((UIElement) target).KeyUp += new KeyEventHandler(this.EditBox_KeyUp);
      ((TxObjEditBoxControl) target).Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.EditBox_Picked);
    }
    else
      this._contentLoaded = true;
  }
}
